package com.sumainfor.zuul.controller.Feign;

import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.zuul.entity.SysUserEntity;
import com.sumainfor.zuul.service.ShiroService;
import com.sumainfor.zuul.service.SysUserService;
import com.sumainfor.zuul.service.impl.Feign.FeignUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 功能描述:
 * 用户
 * @Auther: zhlu
 * @Date: 2019/3/17 17:05
 * @Description:
 */
@Log4j2
@RestController
@RequestMapping("/provider/user")
public class FeignUserController {

    @Autowired
    FeignUserService FeignUserService;

    @Autowired
    SysUserService SysUserService;

    @Autowired
    ShiroService ShiroService;


    /**
     * 功能描述: 
     * 根据用户编号获取用户资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/17 17:08
     */
    @GetMapping("/getUserMap")
    public JsonResult getUserMap(@RequestParam Map<String,Object> params){
        JsonResult result=new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params,new String[]{"UserId"})){
            result.putFailed("UserId不能为空！");
            return  result;
        }
        Map<String,Object>getUserMap=FeignUserService.getUserMap(params);
        result.put(getUserMap);
        return result;
    }

    /**
     * 功能描述: 
     * 获取所有用户的编号和名称
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/18 17:18
     */
    @GetMapping("/getAllUserIdAndName")
    public JsonResult getAllUserIdAndName(){
        JsonResult result=new JsonResult();
        return result.put(FeignUserService.getAllUserIdAndName());
    }

    /**
     * 功能描述: 
     * 根据unionid获取用户资料
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/6/11 19:30
     */
    @GetMapping("/getUserByUnid")
    public JsonResult getUserByUnid(String unionid){
        SysUserEntity user = SysUserService.getUserByUnid(unionid);
        log.info("user----------"+user);
        if (user == null) {
            return JsonResult.error("唐巢內部系統，访问请联系管理员！");
        }
        SysUserEntity entity = ShiroService.queryUser(user.getUserId());
        CustomMap dataResponse = CustomMap.create("userData", entity);
        log.info("dataResponse----------"+dataResponse);
        log.info("JsonResult.success(dataResponse----------"+JsonResult.success(dataResponse));
        return JsonResult.success(dataResponse);
    }


    @PostMapping("/getArea")
    public Map<String,Object> getArea(@RequestBody Map<String,Object> params){
        System.out.println("params"+params);
        return FeignUserService.getArea(params);
    }

}
